package com.other.src.main.java.com.kuang.pc;

import java.util.concurrent.RecursiveTask;

public class A extends RecursiveTask<Long> {
    private Long start;
    private Long end;
    private Long temp = 10000L;

    public A(Long start, Long end) {
        this.start = start;
        this.end = end;

    }

    // forkJoin
    // stream并行流
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 0; i < 10_000_000; i++) {
            sum += i;
        }
    }


    public void increment(String[] args) {

        if ((end - start) > temp) {
            // 分支合并计算

        } else {
            int sum = 0;
            for (int i = 0; i < 10_000_000; i++) {
                sum += i;
            }
            System.out.println(sum);
        }

    }

    @Override
    protected Long compute() {
        if ((end - start) > temp) {
            // 分支合并计算
            Long sum = 0L;
            for (Long i = start; i < end; i++) {
                sum += i;
            }
            return sum;
        } else {
            Long middle = (start + end) / 2;
            A a = new A(start, middle);
            a.fork();//拆分任务，压入
            A a1 = new A(middle + 1, end);
            a1.fork();//拆分任务，压入
            return a.join() + a1.join();
        }
    }
}